
package io.renren.modules.jxgkphone.service.Impl;


import io.renren.modules.jxgk.utils.UUidCreater;
import io.renren.modules.jxgkphone.dao.ClassCourseMapper;
import io.renren.modules.jxgkphone.dao.ClassDiaryMapper;
import io.renren.modules.jxgkphone.entity.ClassCourse;
import io.renren.modules.jxgkphone.entity.ClassDiary;
import io.renren.modules.jxgkphone.service.ClassDiaryService;
import io.renren.modules.jxgkphone.tempEntity.TempClass;
import io.renren.modules.jxgkphone.tempEntity.TempCourse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ClassDiaryServiceImpl implements ClassDiaryService {
    @Autowired
    ClassDiaryMapper classDiaryMapper;
    @Autowired
    ClassCourseMapper classCourseMapper;

    @Override
    public List<TempCourse> SelectAllCourse(String classId, String class_academic_term, String class_academic_year){
        List<TempCourse> tc=classDiaryMapper.SelectAllCourse(classId,class_academic_term,class_academic_year);
        return tc;
    }

    @Override
    public ClassDiary FindDiary(String cid){
        ClassDiary classDiary=classDiaryMapper.FindDiary(cid);
        return  classDiary;
    }

    @Override
    public String UpdateDiary(ClassDiary classDiary){
        //第一步是根据courseId查询数据库里面是否有这次的课堂日志
        UUidCreater u=new UUidCreater();
        ClassCourse cc=classCourseMapper.getCourseById(classDiary.getCourseId());

            if(cc!=null&&"2".equals(cc.getCourseDiaryState())){
            return "已修改，无法再次修改";
        }else {
            if (classDiary.getDiaryState().equals("增课"))//增课
            {
                String diaryId = classDiaryMapper.SelectDiary(classDiary.getCourseId());
                System.out.println(diaryId);
                if (diaryId == null) {
                    //没有日志
                    classDiary.setCourseId(u.getUUID());
                    System.out.println("没有日志");
                    classDiaryMapper.InsertDiary(classDiary);
                    return classDiary.getCourseId();
                } else {
                    System.out.println("有日志");
                    classDiaryMapper.UpdateDiary(classDiary);
                    return classDiary.getCourseId();
                }

            }else {
                String diaryId = classDiaryMapper.SelectDiary(classDiary.getCourseId());
                System.out.println(diaryId);
                if (diaryId == null) {
                    //没有日志
                    System.out.println("没有日志");
                    classDiaryMapper.InsertDiary(classDiary);
                    Integer cds = Integer.parseInt(cc.getCourseDiaryState()) + 1;
                    cc.setCourseDiaryState(cds.toString());
                    classCourseMapper.updateCourseAttandanceState(cc);
                    return cds.toString();
                } else {
                    System.out.println("有日志");
                    classDiaryMapper.UpdateDiary(classDiary);
                    Integer cds = Integer.parseInt(cc.getCourseDiaryState()) + 1;
                    cc.setCourseDiaryState(cds.toString());
                    classCourseMapper.updateCourseAttandanceState(cc);
                    return cds.toString();
                }
            }
        }

    }
    public ClassDiary DiaryInfo(String index,String year,String month,String day){
        return classDiaryMapper.DiaryInfo(index,year,month,day);
    }
    public String CourseTime(String courseId){
        return classDiaryMapper.CourseTime(courseId);
    }

}

